package com.ophiux.cs.api.user.impl;

import java.lang.reflect.Type;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.gson.reflect.TypeToken;
import com.ophiux.cs.api.user.port.IUserApiPort;
import com.ophiux.cs.application.core.AppResultCode;
import com.ophiux.cs.application.core.Request;
import com.ophiux.cs.application.core.Result;
import com.ophiux.cs.application.core.ResultEnum;
import com.ophiux.cs.application.user.dto.D_GetDoctorUserInfoRequest;
import com.ophiux.cs.application.user.dto.D_GetDoctorUserInfoResponse;
import com.ophiux.cs.application.user.dto.LoginRequest;
import com.ophiux.cs.application.user.dto.LoginResponse;
import com.ophiux.cs.application.user.dto.addUserByDoctorRequest;
import com.ophiux.cs.application.user.dto.addUserByDoctorResponse;
import com.ophiux.cs.application.user.dto.addUserInfoRequest;
import com.ophiux.cs.application.user.dto.addUserInfoResponse;
import com.ophiux.cs.application.user.dto.getPatientInfoRequest;
import com.ophiux.cs.application.user.dto.getPatientInfoResponse;
import com.ophiux.cs.application.user.dto.updateUserInfoRequest;
import com.ophiux.cs.application.user.dto.updateUserInfoResponse;
import com.ophiux.cs.application.user.service.IUserAppService;
import com.ophiux.utils.parser.json.JsonUtils;
import com.ophiux.utils.parser.json.JsonUtils.TypeObtainCallback;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class UserApiService implements IUserApiPort {
	@Autowired
	IUserAppService userAppService;

	@Override
	public String Login(String data) {
		log.info("Login接口,请求参数："+data);
		Result<LoginResponse> result = null;
		
		Request<LoginRequest> request = null;
		
		try {
			request = JsonUtils.toEntity(data, new TypeObtainCallback() {
				
				public Type getObtainType() {
					return new TypeToken<Request<LoginRequest>>() {
					}.getType();
				}
			});
			LoginRequest dto =request.getPARAM();
			result = userAppService.Login(dto);
			
			String returnMessage = JsonUtils.toJson(result);
			log.info("Login接口,请求参数："+data+"Login接口,返回参数："+returnMessage);
			return returnMessage;
		} catch (Exception e) {
			log.info("Login接口异常：",e);
			e.printStackTrace();
			result = Result.fail(AppResultCode.EXCEPTION, e.getMessage());
			return JsonUtils.toJson(result);
		}
		
	}

	@Override
	public String P_GetUserInfo(String data) {
		log.info("P_GetUserInfo接口,请求参数："+data);
		Result<getPatientInfoResponse> result = null;
		
		Request<getPatientInfoRequest> request = null;
		
		try {
			request = JsonUtils.toEntity(data, new TypeObtainCallback() {
				
				public Type getObtainType() {
					return new TypeToken<Request<getPatientInfoRequest>>() {
					}.getType();
				}
			});
		
			
			getPatientInfoRequest dto =request.getPARAM();
			result = userAppService.getUserInfoByPatient(dto);
			
			String returnMessage = JsonUtils.toJson(result);
			log.info("P_GetUserInfo接口,返回参数："+returnMessage);
			return returnMessage;
		} catch (Exception e) {
			log.info("P_GetUserInfo接口,请求参数："+data+"P_GetUserInfo接口异常：",e);
			e.printStackTrace();
			result = Result.fail(AppResultCode.EXCEPTION, e.getMessage());
			return JsonUtils.toJson(result);
		}
	}

	@Override
	public String P_Register(String data) {
		log.info("P_Register接口,请求参数："+data);
		Result<addUserInfoResponse> result = null;
		Request<addUserInfoRequest> request = null;
		try {
			request = JsonUtils.toEntity(data, new TypeObtainCallback() {
				
				public Type getObtainType() {
					return new TypeToken<Request<addUserInfoRequest>>() {
					}.getType();
				}
			});
		
			
			addUserInfoRequest dto =request.getPARAM();
			result = userAppService.addUserInfo(dto);
			
			String returnMessage = JsonUtils.toJson(result);
			log.info("P_Register接口,请求参数："+data+"P_Register接口,返回参数："+returnMessage);
			return returnMessage;
		} catch (Exception e) {
			log.info("P_Register接口异常：",e);
			e.printStackTrace();
			result = Result.fail(AppResultCode.EXCEPTION, e.getMessage());
			return JsonUtils.toJson(result);
		}
	}


	@Override
	public String UpdateUserInfo(String data) {
		log.info("UpdateUserInfo接口,请求参数："+data);
		Result<updateUserInfoResponse> result = null;
		Request<updateUserInfoRequest> request = null;
		try {
			request = JsonUtils.toEntity(data, new TypeObtainCallback() {
				public Type getObtainType() {
					return new TypeToken<Request<updateUserInfoRequest>>() {
					}.getType();
				}
			});
			result=userAppService.updateUserInfo(request.getPARAM());

			String returnMessage = JsonUtils.toJson(result);
			log.info("UpdateUserInfo接口,请求参数："+data+"UpdateUserInfo接口,返回参数："+returnMessage);
			return returnMessage;
			
		} catch (Exception e) {
			log.info("UpdateUserInfo接口异常");
			e.printStackTrace();
			result = Result.fail(ResultEnum.requestException.getCode(), ResultEnum.requestException.getMsg());
			return JsonUtils.toJson(result);
		}
	}

	@Override
	public String D_Register(String data) {
		log.info("D_Register接口,请求参数："+data);
		Result<addUserByDoctorResponse> result = null;
		Request<addUserByDoctorRequest> request = null;
		try {
			request = JsonUtils.toEntity(data, new TypeObtainCallback() {
				public Type getObtainType() {
					return new TypeToken<Request<addUserByDoctorRequest>>() {
					}.getType();
				}
			});
			result = userAppService.addUserByDoctor(request.getPARAM());

			String returnMessage = JsonUtils.toJson(result);
			log.info("D_Register接口,请求参数："+data+"D_Register接口,返回参数："+returnMessage);
			return returnMessage;
		} catch (Exception e) {
			log.info("D_Register接口异常");
			e.printStackTrace();
			result = Result.fail(ResultEnum.requestException.getCode(), ResultEnum.requestException.getMsg());
			return JsonUtils.toJson(result);
		}
	}

	@Override
	public String D_GetDoctorUserInfo(String data) {
		log.info("D_GetDoctorUserInfo接口,请求参数："+data);
		Result<D_GetDoctorUserInfoResponse> result = null;
		Request<D_GetDoctorUserInfoRequest> request = null;
		try {
			request = JsonUtils.toEntity(data, new TypeObtainCallback() {
				public Type getObtainType() {
					return new TypeToken<Request<D_GetDoctorUserInfoRequest>>() {
					}.getType();
				}
			});
			result = userAppService.getDoctorUserInfo(request.getPARAM());

			String returnMessage = JsonUtils.toJson(result);
			log.info("D_GetDoctorUserInfo接口,请求参数："+data+"D_GetDoctorUserInfo接口,返回参数："+returnMessage);
			return returnMessage;
		} catch (Exception e) {
			log.info("D_GetDoctorUserInfo接口异常");
			e.printStackTrace();
			result = Result.fail(ResultEnum.requestException.getCode(), ResultEnum.requestException.getMsg());
			return JsonUtils.toJson(result);
		}
	}

}
